import { GlobalObj } from "../obj/GlobalObj";
import { PGCServer } from "../PGCServer";
import { DressUpData } from "./data/DressUpData";
import { DressUpServerDataMgr, DressUpServerTag } from "./data/DressUpServerDataMgr";

export class DressUpDataParse {
    static getProductUrl(goodsData: any): string {
        if(! goodsData)return;
        let desc = JSON.parse("{" + goodsData.preform_attribute_desc + "}");
        if (desc && desc.purl) {
            return PGCServer.ins.getBucketUrl(desc.purl);
        }
    }

    /**
     * 获取自定义属性
     * @param goodsData 装扮数据
     * @param key 自定义的key
     * @returns 
     */
    static getCustomProp(goodsData: any, key:string): any {
        if(! goodsData)return;
        let desc = JSON.parse("{" + goodsData.preform_attribute_desc + "}");
        if (desc) {
            return desc[key];
        }
    }

    static isDress(ids:any, id:number){
        if (typeof ids == "string") {
            return ids.split(",").indexOf(id + "") >= 0;
        }else{
            return ids.indexOf(id) >= 0;
        }
    }
    
    /** 获取icon url */
    static getPlatformImgUrl(goodsData:DressUpData){
        let url = goodsData.preform_img;
        let isDress = goodsData.preform_type == DressUpServerTag.ZHUANG_RONG_TAG;
        if(! url || !isDress || url.indexOf(".png") >=0) return url;
        return url + (GlobalObj.isFa()?"/female.png":"/male.png");
    }
    
    /** 获取画法贴图 */
    static getTextureUrl(url:string){
        if(!url|| url.indexOf(".png") >=0) return url;
        return url + (GlobalObj.isFa()?"/female.png":"/male.png");
    }

    static loadArrJson(arr:DressUpData[], callback:Laya.Handler){
        let tempObj = {count:0};
        function loadJson(dressData:any) {
            let url = PGCServer.ins.formatUrl(dressData.preform_file);
            if(!url || url.indexOf(".dat") >=0){
                tempObj.count --;
                return;
            }
            Laya.loader.load(url, Laya.Handler.create(null, (data: any) => {
                if (data && data.data) {
                    if (data.data.colors && data.data.colors.length) {
                        dressData.isColor = true;// 是颜色
                    }
                    if (dressData.isColor) {
                        dressData.color = data.data.colors;
                    }
                    let idx = url.lastIndexOf("/");
                    dressData.preform_info ||= {};
                    for (const key in data.data) {
                        if (key == "albedoTexture" || key == "dataFile" || key == "maskTexture") { // 资源路径
                            dressData.preform_info[key] = url.substring(0, idx) + "/" + data.data[key];
                        } else {
                            dressData.preform_info[key] = data.data[key];
                            if(key !== "colors")
                                console.warn("出现了新字段:----->" + key, data.data[key]);
                        }
                    }
                }
                tempObj.count --;
                if(tempObj.count <= 0){
                    callback && callback.run();
                }
            }), null, Laya.Loader.JSON);
        }
        
        if(arr && arr.length){
            tempObj.count = arr.length;
            for (let index = 0; index < arr.length; index++) {
                let dress = arr[index];
                if(dress){
                    loadJson(dress);
                }else{
                    tempObj.count --;
                }
            }
            if(tempObj.count <= 0){
                callback && callback.run();
            }
        }else{
            callback && callback.run();
        }
    }
}